package com.googlecode.gwtbb.client.only.widget.form.forum;

import com.googlecode.gwtbb.client.only.remote.ClientSession;
import com.googlecode.gwtbb.client.only.remote.forum.ForumServiceProxy;
import com.googlecode.gwtbb.client.only.widget.form.FormWidget;
import com.googlecode.gwtbb.client.only.widget.form.entry.FormEntry;
import com.googlecode.gwtbb.client.shared.entity.forum.Forum;
import com.googlecode.gwtbb.client.shared.remote.forum.CreateForumBean;
import com.googlecode.gwtbb.client.shared.validation.Failure;
import com.googlecode.gwtbb.client.shared.validation.FieldId;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Apr 30, 2008
 */
public class CreateForumForm extends FormWidget {
	private CreateForumBean createForumBean = new CreateForumBean();

	public CreateForumForm(ClientSession session) {
		super(session);
		init();
	}

	public List<FormEntry> getFormEntries() {
		List<FormEntry> retVal = new ArrayList<FormEntry>(2);
		retVal.add(createLabeledTextBoxFormEntry(Forum.NAME, 0, null, 0, 0, cmss.fieldLabelName()));
		retVal.add(createLabeledTextBoxFormEntry(Forum.DESCRIPTION, 0, null, 1, 0, cmss.fieldLabelDescription()));
		return retVal;
	}

	public String getProcessName() {
		return "createForum";
	}

	public String getProcessSuccess() {
		return cmss.createForumProcessSuccess();
	}

	public String getProcessTitle() {
		return cmss.createForumProcessTitle();
	}

	public Map<FieldId, Failure> validate(Map<FieldId, Failure> failures, int index) {
		createForumBean.setName(getValue(Forum.NAME, 0));
		createForumBean.setDescription(getValue(Forum.DESCRIPTION, 0));
		createForumBean.validate(failures, index);
		setValue(Forum.NAME, 0, createForumBean.getName());
		setValue(Forum.DESCRIPTION, 0, createForumBean.getDescription());
		return failures;
	}

	protected void doAction() {
		ForumServiceProxy.instance().create(session.getUser().getId(), session.getSessionId(), createForumBean, asyncCallback);
	}
}
